package cn.changgou.seckill.task;

import cn.changgou.entity.IdWorker;
import cn.changgou.entity.SeckillStatus;
import cn.changgou.seckill.dao.SeckillGoodsMapper;
import cn.changgou.seckill.dao.SeckillOrderMapper;
import cn.changgou.seckill.pojo.SeckillGoods;
import cn.changgou.seckill.pojo.SeckillOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MultiThreadingCreateOrder {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private IdWorker idWorker;

    @Async
    public void createOrder() {
        try {
            // 从Redis队列中获取用户排队信息
            SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps("SeckillOrderQueue").rightPop();
            if (seckillStatus == null) {
                return;
            }

            // 定义要购买的商品的ID和时区以及用户名字
            String time = seckillStatus.getTime();
            Long id = seckillStatus.getGoodsId();
            String username = seckillStatus.getUsername();
            System.out.println(111111111);
            // 查询秒杀商品
            Thread.sleep(10000);
            String namespace = "SeckillGoods_" + time;
            SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps(namespace).get(id);
            // 判断有没有库存
            if (seckillGoods == null || seckillGoods.getStockCount() <= 0) {
                throw new RuntimeException("已售罄！");
            }
            // 创建订单对象
            SeckillOrder seckillOrder = new SeckillOrder();
            seckillOrder.setId(idWorker.nextId());
            // 商品ID
            seckillOrder.setSeckillId(id);
            // 支付金额
            seckillOrder.setMoney(seckillGoods.getCostPrice());
            // 用户名
            seckillOrder.setUserId(username);
            // 创建时间
            seckillOrder.setCreateTime(new Date());
            // 未支付
            seckillOrder.setStatus("0");
            /*
             * 将订单对象存储起来
             * 1. 一个用户只允许有一个未支付秒杀订单
             * 2. 订单存入到Redis
             * */
            redisTemplate.boundHashOps("SeckillOrder").put(username, seckillOrder);
            /*
             * 库存递减
             * 商品可能是最后一个，若是最后一个，则将Redis中商品信息删除，并且将Redis中该商品同步到Mysql
             * */
            seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
            if (seckillGoods.getStockCount() <= 0) {
                // 同步数据到Mysql
                seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
                // 删除Redis中的商品数据
                redisTemplate.boundHashOps(namespace).delete(id);
            } else {
                // 同步数据到Redis
                redisTemplate.boundHashOps(namespace).put(id, seckillGoods);
            }
            // 更新下单状态
            seckillStatus.setOrderId(seckillOrder.getId());
            // 支付金额
            seckillStatus.setMoney(seckillGoods.getCostPrice());
            // 待付款
            seckillStatus.setStatus(2);
            redisTemplate.boundHashOps("UserQueueStatus").put(username, seckillStatus);
            System.out.println(22222222);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
